package com.cloudfun.campusshare.service.handler;

import com.cloudfun.campusshare.common.constant.AttachType;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.DeviceType;
import com.cloudfun.campusshare.common.constant.WorkerOrderTaskType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.vo.AttachVO;
import com.cloudfun.campusshare.common.model.vo.RepairWorkerOrderDetailVO;
import com.cloudfun.campusshare.common.model.vo.WorkerOrderResolveRecordVO;
import com.cloudfun.campusshare.entity.*;
import com.cloudfun.campusshare.repo.*;
import com.cloudfun.campusshare.service.AttachmentService;
import com.cloudfun.campusshare.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.bind.annotation.XmlType;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/5/11 19:52
 * @description :
 */
@Component
@Order(1)
public class RepairWorkerOrderHandler implements WorkerOrderHandler<RepairWorkerOrderDetailVO> {
    @Resource
    private RepairEntityRepo repairEntityRepo;
    @Resource
    private DeviceEntityRepo deviceEntityRepo;
    @Resource
    private UserEntityRepo userEntityRepo;

    @Autowired
    private AttachmentService attachmentService;

    @Resource
    private WorkerOrderResolveRecordEntityRepo workerOrderResolveRecordEntityRepo;

    @Resource
    private WashPointConcretePositionEntityRepo washPointConcretePositionEntityRepo;

    @Override
    public RepairWorkerOrderDetailVO handle(WorkerOrderEntity workerOrderEntity) {
        RepairWorkerOrderDetailVO result = new RepairWorkerOrderDetailVO();
        result.setId(workerOrderEntity.getId());
        result.setTransactorId(workerOrderEntity.getTransactorId());
        result.setTransactorName(workerOrderEntity.getTransactorName());
        result.setWorkerOrderStatus(workerOrderEntity.getWorkerOrderStatus().name());
        result.setWorkerOrderStatusName(workerOrderEntity.getWorkerOrderStatus().getMessage());
        RepairEntity repairEntity = repairEntityRepo.findById(workerOrderEntity.getBusinessId()).orElseThrow(() -> new BusinessException(Code.NODATA));
        DeviceEntity device = deviceEntityRepo.findById(repairEntity.getDeviceId()).orElseThrow(() -> new BusinessException(Code.NODATA));
        //设备信息
        result.setDeviceName(device.getDeviceName());
        if(StringUtils.isNotBlank(device.getConcretePositionId())){
            Optional<WashPointConcretePositionEntity> con = washPointConcretePositionEntityRepo.findById(device.getConcretePositionId());
            if(con.isPresent()){
                result.setBuilding(con.get().getBuilding());
                result.setFloor(con.get().getFloor());
                result.setRoom(con.get().getRoom());
            }
        }
        // modify at 2021-05-28
        result.setWashPointName(workerOrderEntity.getWashPointName());
        result.setImei(device.getImei());

        result.setBelongMerchantName(device.getBelongMerchantName());
        result.setBrandName(device.getBrandName());
        result.setBrandServicePhone(Optional.ofNullable(device.getDeviceBrandEntity()).map(x -> x.getServicePhone()).orElse(""));
        result.setProductType(Objects.isNull(device.getProductType()) ? "" : device.getProductType().getDesc());
        result.setDeviceType(device.getDeviceType());
        result.setDeviceTypeName(DeviceType.getDescByName(device.getDeviceType()));
        //报修信息
        result.setExceptionPosition(repairEntity.getFaultType());
        result.setFaultDesc(repairEntity.getFaultDesc());
        result.setSubmitTime(DateUtil.localDateToString(workerOrderEntity.getSubmitTime()));
        result.setExpireTime(DateUtil.localDateToString(workerOrderEntity.getExpireTime()));
        result.setOpenedTime(DateUtil.computeBetweenLocalDate(workerOrderEntity.getSubmitTime(), LocalDateTime.now()));
        UserEntity userEntity = userEntityRepo.findById(repairEntity.getUserId()).orElseThrow(() -> new BusinessException(Code.NODATA));
        result.setSubmitCustomer(userEntity.getId());
        result.setSubmitAccount(userEntity.getPhone());
        result.setRepairResult(Objects.isNull(repairEntity.getRepireResult()) ? "" : repairEntity.getRepireResult().name());
        result.setRemark(repairEntity.getRemark());
        List<AttachVO> attachment = attachmentService.getAttachment(repairEntity.getId(), AttachType.DEVICE_REPAIR);
        result.setImages(attachment.stream().map(AttachVO::getUrl).collect(Collectors.toList()));
        result.setRecordList(workerOrderResolveRecordEntityRepo.findAllByWorkerOrderId(workerOrderEntity.getId()).stream().map(x -> {
            WorkerOrderResolveRecordVO workerOrderResolveRecordVO = new WorkerOrderResolveRecordVO();
            workerOrderResolveRecordVO.setId(x.getId());
            workerOrderResolveRecordVO.setWorkerOrderId(x.getWorkerOrderId());
            workerOrderResolveRecordVO.setResolveTime(DateUtil.localDateToString(x.getResolveTime()));
            workerOrderResolveRecordVO.setResolver(x.getResolver());
            workerOrderResolveRecordVO.setWorkerOperateType(x.getWorkerOperateType().getMessage());
            workerOrderResolveRecordVO.setConclusion(x.getConclusion());
            return workerOrderResolveRecordVO;
        }).collect(Collectors.toList()));
        return result;
    }

    @Override
    public WorkerOrderTaskType supportType() {
        return WorkerOrderTaskType.DEVICE_REPAIR;
    }
}
